#include <iostream>
#include <cstring>

using namespace std;

class String
{
private:
    char *str;
    size_t length;

public:
    // 构造函数
    String();  
    // 构造函数，初始化字符串为给定的字符串副本 
    String(const char *s);
    // 拷贝构造函数
    String(const String &s);
    ~String();
    // 获取字符串长度
    size_t getLength() const;       
    // 获取字符串的索引（运算符重载）                           
    char operator[](size_t index) const; 
    // 赋值运算符重载                      
    String &operator=(const String &s);  
    // 等于运算符                      
    bool operator==(const String &s) const;    
    // 不等于运算符                
    bool operator!=(const String &s) const;   
    // 复制                 
    String copy() const;  
    // 拼接                                     
    String operator+(const String &s) const;     
    // 友元函数，用来作为输出              
    friend ostream &operator<<(ostream &out, const String &s); 
};

String::String()
{
    length = 0;
    str = new char[1];
    str[0] = '\0';
}

String::String(const char *s)
{
    length = strlen(s);
    str = new char[length + 1];
    strcpy(str, s);
}

String::String(const String &s)
{
    length = s.length;
    str = new char[length + 1];
    strcpy(str, s.str);
}

String::~String()
{
    delete[] str;
}

size_t String::getLength() const
{
    return length;
}

char String::operator[](size_t index) const
{
    return str[index];
}

String &String::operator=(const String &s)
{
    if (this != &s)
    {
        delete[] str;
        length = s.length;
        str = new char[length + 1];
        strcpy(str, s.str);
    }
    return *this;
}

bool String::operator==(const String &s) const
{
    return strcmp(str, s.str) == 0;
}

bool String::operator!=(const String &s) const
{
    return strcmp(str, s.str) != 0;
}

String String::copy() const 
{
    String s = *this;
    return s;
}

String String::operator+(const String &s) const 
{
    String result;
    result.length = length + s.length;
    result.str = new char[result.length + 1];
    strcpy(result.str, str);
    strcat(result.str, s.str);
    return result;
}

ostream &operator<<(ostream &out, const String &s)
{
    out << s.str;
    return out;
}

int main()
{
    String s("asdga23");
    cout << s << endl;
    cout << s + "000" << endl;
    cout << s[5] << endl;
    cout << (s == "asdga23") << endl;
    cout << (s != "asdga23") << endl;
    s = s + "asd";
    String a, b, c;
    a = b = c = s;
    cout << a << b << c << s << endl;
    String x = s.copy();
    cout << s << x << endl;
}